package com.shire.shire_interface.controller.tag;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.group.WxGroup;
import com.shire.shire_interface.model.group.WxGroupExample;
import com.shire.shire_interface.model.tag.WxTag;
import com.shire.shire_interface.model.tag.WxTagExample;
import com.shire.shire_interface.model.tag.WxTagRecord;
import com.shire.shire_interface.model.tag.WxTagRecordExample;
import com.shire.shire_interface.service.group.WxGroupService;
import com.shire.shire_interface.service.tag.WxTagRecordService;
import com.shire.shire_interface.service.tag.WxTagService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//@Api(tags = "群管理 群标签")
@RestController
@RequestMapping("/tag")
public class WxTagRecordController extends BaseController {

    @Resource
    private WxTagService wxTagService;
    @Resource
    private WxTagRecordService wxTagRecordService;
    @Resource
    private WxGroupService wxGroupService;

//    @ApiOperation(value = "标签记录 csId, wxAccount")
    @ResponseBody
    @PostMapping("/list")
    public Response getList(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            WxTagExample wxTagExample = new WxTagExample();
            wxTagExample.createCriteria().andCsidEqualTo(csId).andWxaccuntEqualTo(wxAccount).andTypeEqualTo(1);
            wxTagExample.setOrderByClause("id desc");
            List<WxTag> wxTags = wxTagService.selectByExample(wxTagExample);

            WxTagRecordExample wxTagRecordExample = new WxTagRecordExample();
            WxGroupExample wxGroupExample = new WxGroupExample();
            for (WxTag wxTag : wxTags) {
                wxTagRecordExample.clear();
                Integer tagId = wxTag.getId();
                wxTagRecordExample.createCriteria().andWxacountEqualTo(wxAccount).andTagidEqualTo(tagId);
                List<WxTagRecord> wxTagRecords = wxTagRecordService.selectByExample(wxTagRecordExample);

                List<WxGroup> wxGroupList = new ArrayList<>();
                for (WxTagRecord wxTagRecord : wxTagRecords) {
                    wxGroupExample.clear();
                    String friendGroupId = wxTagRecord.getFriendGroupId();
                    if (StringUtils.isBlank(friendGroupId)){
                        continue;
                    }
                    wxGroupExample.createCriteria().andWxaccountEqualTo(wxAccount).andGroupnoEqualTo(friendGroupId);

                    List<WxGroup> wxGroups = wxGroupService.selectByExample(wxGroupExample);
                    if (wxGroups.size()>0){
                        WxGroup wxGroup = wxGroups.get(0);
                        wxGroupList.add(wxGroup);
                    }
                }
                wxTag.setGroupList(wxGroupList);
            }

            response.setData(wxTags);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "删除记录 id, csId, wxAccount")
    @ResponseBody
    @PostMapping("/del")
    public Response del(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer id = jsonObject.getInteger("id");
            Integer csId = jsonObject.getInteger("csId");
            String wxAccount = jsonObject.getString("wxAccount");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            wxTagService.delByPrimaryKey(id);

            WxTagRecordExample wxTagRecordExample = new WxTagRecordExample();
            wxTagRecordExample.createCriteria().andTagidEqualTo(id).andWxacountEqualTo(wxAccount);
            wxTagRecordService.deleteByExample(wxTagRecordExample);


            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
